home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1996 June / EnigmA AMIGA RUN 08 (1996)(G.R. Edizioni)(IT)[!][issue 1996-06][EARSAN CD VII].iso / earcd / gcc / ixemlsrc.lha / ixemul / library / __fselect.c < prev    next >
C/C++ Source or Header  |  1996-03-13  |  4KB  |  114 lines

  1. /*
  2.  *  This file is part of ixemul.library for the Amiga.
  3.  *  Copyright (C) 1991, 1992  Markus M. Wild
  4.  *
  5.  *  This library is free software; you can redistribute it and/or
  6.  *  modify it under the terms of the GNU Library General Public
  7.  *  License as published by the Free Software Foundation; either
  8.  *  version 2 of the License, or (at your option) any later version.
  9.  *
  10.  *  This library is distributed in the hope that it will be useful,
  11.  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  12.  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  13.  *  Library General Public License for more details.
  14.  *
  15.  *  You should have received a copy of the GNU Library General Public
  16.  *  License along with this library; if not, write to the Free
  17.  *  Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  18.  *
  19.  *  __fselect.c,v 1.1.1.1 1994/04/04 04:29:45 amiga Exp
  20.  *
  21.  *  __fselect.c,v
  22.  * Revision 1.1.1.1  1994/04/04  04:29:45  amiga
  23.  * Initial CVS check in.
  24.  *
  25.  *  Revision 1.2  1993/11/05  21:49:09  mw
  26.  *  socket-changes
  27.  *
  28.  *  Revision 1.1  1992/05/14  19:55:40  mwild
  29.  *  Initial revision
  30.  *
  31.  */
  32.  
  33. #define KERNEL
  34. #include "ixemul.h"
  35. #include "kprintf.h"
  36.  
  37. #include "select.h"
  38.  
  39. /* don't use the `normal' packet port for select. We need synchronous
  40.  * notification to be able to Wait() for multiple replies */
  41.  
  42. /*
  43.  * select operation on a "normal" AmigaDOS filehandle. Normal means,
  44.  * we only have the WaitForChar() call available to find out, whether
  45.  * a read would block. Write() cannot be caught, so we always allow it.
  46.  * Request for exceptional data is routed to read.
  47.  */
  48.  
  49. int
  50. __fselect (struct file *f, int select_cmd, int io_mode)
  51. {
  52.   int result;
  53.  
  54.   if (select_cmd == SELCMD_PREPARE)
  55.     {
  56.       /* always possible... perhaps return ~0 in this case ???? */
  57.       if (io_mode != SELMODE_IN) return 0;
  58.  
  59.       /* should not wait here, but I have to.. the only case that a packet
  60.        * could be outstanding, is if this is a file open for read and write,
  61.        * and an async write is outstanding. Since I'm testing for read I have
  62.        * to wait for write() to complete */
  63.       __get_file (f);
  64.       __wait_packet (&f->f_sp);
  65.       LastError(f) = 0;
  66.  
  67.       SendPacket1(f, __srwport, ACTION_WAIT_CHAR, SELTIMEOUT);
  68.       return 1 << __srwport->mp_SigBit;
  69.     }
  70.   else if (select_cmd == SELCMD_CHECK)
  71.     {
  72.       /* only read is supported, other modes default to `ok' */
  73.       if (io_mode != SELMODE_IN) return 1;
  74.  
  75.       __wait_sync_packet (&f->f_sp);
  76.       /* there are two possible answers: error (packet not supported) 
  77.        * and the `real' answer.
  78.        * An error is treated as to allow input, so select() won't block
  79.        * indefinitely...
  80.        * & 1 converts dos-true (-1) into normal true (1) ;-)
  81.        */
  82.       result = LastError(f) ? 1 : (LastResult(f) & 1);
  83.       /* don't make __write() think its last packet failed.. */
  84.       LastError(f) = 0;
  85.       __release_file (f);
  86.       return result;
  87.     }
  88.   else if (select_cmd == SELCMD_POLL)
  89.     {
  90.       /* only read is supported, other modes default to `ok' */
  91.       if (io_mode != SELMODE_IN) return 1;
  92.  
  93.       __get_file (f);
  94.       __wait_packet (&f->f_sp);
  95.  
  96.       LastError(f) = 0;
  97.       SendPacket1(f, __srwport, ACTION_WAIT_CHAR, 0);
  98.       __wait_sync_packet (&f->f_sp);
  99.       /* there are two possible answers: error (packet not supported) 
  100.        * and the `real' answer.
  101.        * An error is treated as to allow input, so select() won't block
  102.        * indefinitely...
  103.        * & 1 converts dos-true (-1) into normal true (1) ;-)
  104.        */
  105.       result = LastError(f) ? 1 : (LastResult(f) & 1);
  106.       /* don't make __write() think its last packet failed.. */
  107.       LastError(f) = 0;
  108.       __release_file (f);
  109.       return result;
  110.     }
  111.   else
  112.     return 0;
  113. }
  114.